/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.win32;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.kommando.core.util.Assert;
import org.kommando.filesystem.FileSystemBrowser;
import org.kommando.filesystem.FileSystemBrowserException;

/**
 * {@link FileSystemBrowser} implementation for Windows Explorer.
 * 
 * @author Peter De Bruycker
 */
public class WindowsExplorer implements FileSystemBrowser {

    private String grabSelectedFile;

    public WindowsExplorer(String grabSelectedFile) {
        this.grabSelectedFile = grabSelectedFile;
    }

    @Override
    public void reveal(String path) throws FileSystemBrowserException {
        Assert.argumentNotEmpty("path", path);

        try {
            // explorer arguments are separated by commas
            new ProcessBuilder("explorer", "/e,", "/select,", path).start();
        } catch (IOException e) {
            throw new FileSystemBrowserException("Error while trying to reveal " + path, e);
        }
    }

    @Override
    public String getSelectedFile() throws FileSystemBrowserException {
        try {
            Process process = new ProcessBuilder(System.getenv("comspec"), "/c", grabSelectedFile, "|", "find", "/V", "\"\"").start();

            InputStream in = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line = reader.readLine();
            reader.close();

            return line;
        } catch (IOException e) {
            throw new FileSystemBrowserException("Error while getting selected file", e);
        }
    }
}
